package com.hk.demo.player.grid;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hk.demo.player.R;
import com.hk.player.PlayerView;
import com.hk.player.core.MonitorMediaData;
import com.zhoug.common.utils.LogUtils;
import com.zhoug.common.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 网格视频适配器
 *
 * @author: zhoug
 * @since: 2025-08-18
 */
public class VideoGridAdapter extends RecyclerView.Adapter<VideoGridAdapter.VideoViewHolder> {
    private static final String TAG = ">>>VideoGridAdapter";
    private RecyclerView mRecyclerView;
    private int mParentPosition = -1;
    private final Context mContext;
    private final int mSpanCount;
    private final int mSpacing;
    private int mParentHeight;
    private final List<MonitorMediaData> mData = new ArrayList<>();

    public VideoGridAdapter(Context context, int spanCount, int spacing) {
        this.mContext = context;
        this.mSpanCount = spanCount;
        this.mSpacing = spacing;
    }


    public void setParentHeight(int parentHeight) {
        if (this.mParentHeight != parentHeight && parentHeight > 0) {
            this.mParentHeight = parentHeight;
//            LogUtils.d(TAG, "setParentHeight:"+mParentHeight);
        }
    }


    public void setParentPosition(int parentPosition) {
        this.mParentPosition = parentPosition;
    }


    void setData(List<MonitorMediaData> data) {
        this.mData.clear();
        if (data != null && !data.isEmpty()) {
            this.mData.addAll(data);
        }
    }

    private MonitorMediaData getItemData(int position) {
        if (position >= 0 && position < mData.size()) {
            return mData.get(position);
        }
        return null;
    }

    private String getPositionInParent(int position) {
        return mParentPosition + "-" + position;
    }


    @NonNull
    @Override
    public VideoGridAdapter.VideoViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//        LogUtils.d(TAG, "onCreateViewHolder:");
        View view = LayoutInflater.from(mContext).inflate(R.layout.grid_player_page_item, parent, false);
        return new VideoGridAdapter.VideoViewHolder(view);
    }



    @Override
    public void onBindViewHolder(@NonNull VideoGridAdapter.VideoViewHolder holder, int position) {
//        LogUtils.d(TAG, "onBindViewHolder:" + getPositionInParent(position));
        holder.notifyItemHeightChange();
        holder.bindVideo(position);
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    @Override
    public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        LogUtils.d(TAG, "onAttachedToRecyclerView:");
        mRecyclerView = recyclerView;
    }

    class VideoViewHolder extends RecyclerView.ViewHolder {
        private final PlayerView mPlayerView;
        private int mPosition;

        VideoViewHolder(View itemView) {
            super(itemView);
            mPlayerView = itemView.findViewById(R.id.playerView);
        }

        void bindVideo(int position) {
            // 这里实现视频播放逻辑
            this.mPosition = position;
            String positionInParent = getPositionInParent(position);
            MonitorMediaData itemData = getItemData(position);
            LogUtils.d(TAG, positionInParent + ": mPlayerView:" + mPlayerView);
            if (itemData != null) {
                mPlayerView.setMonitorData(mData.get(position));
            }

        }

        void notifyItemHeightChange() {
            if (mRecyclerView == null) return;
            if (mParentHeight > 0) {
                // 计算每个网格项的高度
                int rowCount = mSpanCount;
                int totalSpacing = (rowCount - 1) * mSpacing;
//                totalSpacing=0;
                int itemHeight = (mParentHeight - totalSpacing) / rowCount;
                // 设置网格项高度
                ViewGroup.LayoutParams params = itemView.getLayoutParams();
                if (itemHeight != params.height) {
                    params.height = itemHeight;
                    itemView.setLayoutParams(params);
//                LogUtils.d(TAG, "onBindViewHolder:itemHeight=" + itemHeight + ",totalSpacing=" + totalSpacing);
                }
            }
        }


        void startPlay() {
            if (mPlayerView != null) {
                LogUtils.d(TAG, StringUtils.format("开始播放第%s个视频", getPositionInParent(mPosition)));
                mPlayerView.startPlay();
            }
        }

        void stopPlay() {
            if (mPlayerView != null) {
                LogUtils.d(TAG, StringUtils.format("停止播放第%s个视频", getPositionInParent(mPosition)));
                mPlayerView.stopPlay();
            }
        }

        void pausePlay() {
            if (mPlayerView != null) {
                LogUtils.d(TAG, StringUtils.format("暂停播放第%s个视频", getPositionInParent(mPosition)));
                mPlayerView.pausePlay();
            }
        }

        void releasePlayer() {
            if (mPlayerView != null) {
                LogUtils.d(TAG, StringUtils.format("释放第%s个播放器资源", getPositionInParent(mPosition)));
                mPlayerView.releasePlayer();
            }
        }

        private void setupPlayer(String videoUrl) {
            // 实现视频播放器初始化
            // 建议使用ExoPlayer或MediaPlayer
            // 注意处理视频比例和全屏问题
        }
    }

}
